package com.xiangty.reader;

import com.xiangty.bean.CustTemp;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyBatisCursorItemReaderConfiguration {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Bean("custTempMyBatisCursorItemReader")
    @StepScope
    public MyBatisCursorItemReader<CustTemp> custTempMyBatisCursorItemReader(
            @Value("#{stepExecutionContext[fromId]}") final String fromId,
            @Value("#{stepExecutionContext[toId]}") final String toId
    ) throws Exception {
        System.out.println("----custTempMyBatisCursorItemReader----fromId:" + fromId + "------toId:" + toId);

        MyBatisCursorItemReader reader = new MyBatisCursorItemReader();
        reader.setQueryId("com.xiangty.mapper.CustTempMapper.selectPartitionList3");
        Map<String, Object> map = new HashMap<>();
        map.put("fromId", fromId);
        map.put("toId", toId);
        reader.setParameterValues(map);
        reader.setSqlSessionFactory(sqlSessionFactory);
        reader.setName("custTempMyBatisCursorItemReaderName1");
//        reader.setMaxItemCount(1000);  // 需要设置的大于分片数量，或者不要设置
        reader.afterPropertiesSet();
        return reader;
    }

}
